package com.tuyan.aixiang.activity.startup;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;

import com.tuyan.aixiang.R;
import com.tuyan.aixiang.activity.channel.ChannelActivity;
import com.tuyan.aixiang.common.StorageKey;
import com.tuyan.aixiang.component.StartPage;
import com.tuyan.aixiang.util.StorageUtil;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

public class StartupPagerAdapter extends PagerAdapter {

    List<StartPage> pageList = new ArrayList<>();

    private Context context;

    int[] titles = {R.drawable.s1t, R.drawable.s2t, R.drawable.s3t};
    int[] mains = {R.drawable.s1m, R.drawable.s2m, R.drawable.s3m};
    int[] indicators = {R.drawable.s1i, R.drawable.s2i, R.drawable.s3i};

    public StartupPagerAdapter(Context context) {
        this.context = context;
//        for (int i = 0; i < titles.length; i++) {
//            StartPage iv = new StartPage(context);
//            iv.setup(titles[i], mains[i], indicators[i]);
//            pageList.add(iv);
//        }
    }


    @Override
    public int getCount() {
        return titles.length;
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        StartPage iv =  new StartPage(context); // pageList.get(position);
        iv.setup(titles[position], mains[position], indicators[position]);
        container.addView(iv);
        if (position == titles.length - 1) {
            iv.setStartCallback(() -> {
                StorageUtil.setBoolean(StorageKey.FIRST_START, false);
                context.startActivity(new Intent(context, ChannelActivity.class));
                ((Activity)context).finish();
            });

        }
        return iv;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }
}
